<?php
namespace Home\Controller;

use Common\Controller\MemberbaseController;
use Common\Lib\RedisHandle;
use Common\Model\MembersModel;
use Common\Model\MerchantModel;
use Common\Model\PromotionCoupon;
use Common\Model\PromotionCouponBannersModel;
use Common\Model\PromotionCouponCodeModel;
use Common\Model\PromotionCouponModel;
use Common\ModelSQ\ERPUser;

class MemberController extends MemberbaseController
{

    public function _initialize()
    {
        parent::_initialize();
    }

    //我的会员
    public function Index()
    {
        $memberModel = new MembersModel();

        $cache_member = $this->getMember();
        $member = $memberModel->getMemberByMobile($cache_member['telephone']);
        if (!strstr($member['head_img'], 'http')) {
            $member['head_img'] = C('UPLOADPATH') . $member['head_img'];
        }

        if (!isset($member['integral'])) {
            $member['integral'] = 0;
        }
        $member['coupon'] = M('promotion_coupon_code')
                    ->alias('pcc')
                    ->field('pcc.create_time ,pc.coupon_name')
                    ->join('xd_promotion_coupon pc ON pc.id = pcc.coupon_id')
                    ->where(array('member_id'=>$member['id']))
                    ->order('pcc.id desc')
                    ->select();
        $this->assign('member', $member);
        $this->display();
    }

    //我的过期卡片
    public function CardOverDue()
    {

        $couponModel = new PromotionCouponModel();
        $couponCodeModel = new PromotionCouponCodeModel();
        $member = $this->getMember();
        $cur_date = date('Y-m-d H:i:s');

        $coupons = $couponCodeModel->where(array('use_flg' => 1, 'member_id' => $member['id'], 'end_date' => array('elt', $cur_date)))->select();
        if ($coupons) {
            foreach ($coupons as &$coupon) {
                $tmp_coupon = $couponModel->where(array('id' => $coupon['coupon_id']))->find();
                $coupon['card_img'] = $tmp_coupon['card_img'];
            }
        }

        $this->assign('coupons', $coupons);

        $this->display();
    }

    //我的卡片
    public function MyCard()
    {
        $couponModel = new PromotionCouponModel();
        $couponCodeModel = new PromotionCouponCodeModel();
        $member = $this->getMember();
        $cur_date = date('Y-m-d H:i:s');


        $coupons = $couponCodeModel->where(array('use_flg' => 1, 'member_id' => $member['id'], 'use_date IS NULL', 'end_date' => array('gt', $cur_date)))->select();
        if ($coupons) {
            foreach ($coupons as &$coupon) {
                $tmp_coupon = $couponModel->where(array('id' => $coupon['coupon_id']))->find();
                $coupon['card_img'] = $tmp_coupon['card_img'];
            }
        }

        $this->assign('coupons', $coupons);
        $this->display();
    }

    //卡片详情
    public function CardVerification()
    {

        $couponId = I("get.id");
        $couponModel = new PromotionCouponModel();
        $couponCodeModel = new PromotionCouponCodeModel();
        $member = $this->getMember();


        $coupon = $couponCodeModel->where(array('id' => $couponId, 'use_flg' => 1, 'member_id' => $member['id']))->find();
        if ($coupon) {
            $tmp_coupon = $couponModel->where(array('id' => $coupon['coupon_id']))->find();
            $coupon['card_img'] = $tmp_coupon['card_img'];
            $coupon['description'] = $tmp_coupon['description'];

            $no = $coupon['code'];
            preg_match('/([A-Za-z0-9]{4})([A-Za-z0-9]{4})([A-Za-z0-9]{4})([A-Za-z0-9]{0,})?/', $no, $match);
            unset($match[0]);
            $coupon['code_show'] = implode(' ', $match);
        }
        $this->assign('coupon', $coupon);
        $this->display();
    }

    //会员权益
    public function MemberInterests()
    {

        $this->display();
    }

    //会员福利
    public function memberWelfare()
    {
        $sort = I('sort', 0);
        //banner
        $banners = M('banners')->where(array('use_flg' => 1, 'banner_type' => 'welfare'))->order('sort ASC')->select();
        $this->assign('banners', $banners);

        //优惠券
        $curDatetime = date('Y-m-d H:i:s');
        $couponModel = new PromotionCouponModel();

        $order = array();
        if($sort == 1) $order = array('sort' => 'ASC');
        if($sort == 2) $order = array('exintegral' => 'ASC');

        $coupons = $couponModel->where(array('use_flg' => 1))->order($order)->select();
        //$coupons = $couponModel->where(array('use_flg'=>1,'start_date'=>array('elt',$curDatetime),'end_date'=>array('egt',$curDatetime)))->order('sort ASC')->select();

        $this->assign('sort', $sort);
        $this->assign('coupons', $coupons);
        $this->display();
    }

    //福利详情
    public function welfareDetail()
    {
        $couponModel = new PromotionCouponModel();


        $id = I('get.couponid');

        //优惠券

        $coupon = $couponModel->where(array('id' => $id))->find();

        //banner
        $couponBannerModel = new PromotionCouponBannersModel();
        $banners = $couponBannerModel->where(array('coupon_id' => $coupon['id']))->select();
        $coupon['banners'] = $banners;

        //商户
        $merchantModel = new MerchantModel();
        $merchant = $merchantModel->where(array('id' => $coupon['merchant_id']))->find();

        $this->assign('coupon', $coupon);
        $this->assign('merchant', $merchant);
        $this->display();
    }

    //修改密码
    public function modifyPwd()
    {

        $this->display();
    }

    public function doExCoupon()
    {

        $member = $this->getMember();
        $couponId = I('post.couponid');
        $couponModel = new PromotionCouponModel();
        $result = $couponModel->receiveCoupon($couponId, $member['id'],$member);


        if ($result == 200) {
            $this->ajaxReturn(array('code' => 200, 'message' => '成功'));
        } else if ($result == 401) {
            $this->ajaxReturn(array('code' => 401, 'message' => '该福利已被领完'));
        } else if ($result == 402) {
            $this->ajaxReturn(array('code' => 402, 'message' => '积分不足'));
        } else if ($result == 404) {
            $this->ajaxReturn(array('code' => 404, 'message' => '该福利不存在'));
        } else {
            $this->ajaxReturn(array('code' => 500, 'message' => '系统错误,请重试！'));
        }

    }


    public function doModifyPwd()
    {
        $old_password = I("post.old_password");
        $new_password = I("post.new_password");
        $memberModel = new MembersModel();
        $cache_member = $this->getMember();
        $member = $memberModel->getMemberByMobile($cache_member['telephone']);
        if ($member['password'] != md5($old_password)) {
            $this->ajaxReturn(array('code' => 400, 'message' => '原密码不正确,请重新输入'));
        }
        $data = array(
            'password' => md5($new_password)
        );
        $memberModel->modify($member['telephone'], $data);
        $this->ajaxReturn(array('code' => 200, 'message' => '成功'));

    }


    public function MemberDetail()
    {

        $memberModel = new MembersModel();


        $cache_member = $this->getMember();
        $member = $memberModel->getMemberByMobile($cache_member['telephone']);
        if (!strstr($member['head_img'], 'http')) {
            $member['head_img'] = C('UPLOADPATH') . $member['head_img'];
        }

        if (!isset($member['integral'])) {
            $member['integral'] = 0;
        }
        $member['telephone'] = substr_replace($member['telephone'], '****', 3, 4);

        $this->assign('member', $member);
        $this->display();
    }


    public function doModify()
    {
        $erpUser = new ERPUser();
        $api_config = C('ERPUSER');
        $params = I('post.');
        $member = $this->getMember();


        //会员同步修改
        $erp_member_result = $erpUser->getMember(array('telephone' => $member['telephone']));
        if ($erp_member_result['success'] && $erp_member_result['code'] == 1000) {
            $erp_member = array(
                'issue_store' => $erp_member_result['data']['issue_store'],
                'telephone' => $erp_member_result['data']['telephone'],
                'member_name_chn' => $erp_member_result['data']['member_name_chn'],
                'sex' => $erp_member_result['data']['sex'],
                'address' => $erp_member_result['data']['address'],
                'postal_code' => $erp_member_result['data']['postal_code'],
                'email' => $erp_member_result['data']['email'],
                'certificate_type' => $erp_member_result['data']['certificate_type'],
                'id_no' => $erp_member_result['data']['id_no'],
                'member_code' => $erp_member_result['data']['member_code']
            );


            foreach ($params as $key => $val) {
                $erp_member[$key] = $val;
            }
            $erpUser->updateMember($erp_member);
        }

        $memberModel = new MembersModel();
        $memberModel->modify($member['telephone'], $params);
        $this->ajaxReturn(array('code' => 200, 'message' => '成功'));
    }

    //自足积分
    public function selfHelp()
    {
        $memberModel = new MembersModel();


        $cache_member = $this->getMember();
        $member = $memberModel->getMemberByMobile($cache_member['telephone']);
        if (!strstr($member['head_img'], 'http')) {
            $member['head_img'] = C('UPLOADPATH') . $member['head_img'];
        }

        if (!isset($member['integral'])) {
            $member['integral'] = 0;
        }
        $member['telephone'] = substr_replace($member['telephone'], '****', 3, 4);

        $no = $member['member_code'];
        preg_match('/([A-Za-z0-9]{4})([A-Za-z0-9]{4})([A-Za-z0-9]{4})([A-Za-z0-9]{0,})?/', $no, $match);
        unset($match[0]);
        $member['member_code_show'] = implode(' ', $match);

        $this->assign('member', $member);
        $this->display();
    }
}


